package com.sskj.xgja.market;

import android.content.Context;
import android.content.Intent;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.KeyEvent;
import android.view.View;
import android.view.inputmethod.EditorInfo;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.hjq.toast.ToastUtils;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;
import com.sskj.base.adapter.BaseAdapter;
import com.sskj.base.adapter.ViewHolder;
import com.sskj.base.utils.ClickUtil;
import com.sskj.base.view.DividerLineItemDecoration;
import com.sskj.xgja.MVPActivity;
import com.sskj.xgja.R;
import com.sskj.xgja.main.MainActivity;
import com.sskj.xgja.market.bean.MarketData;

import java.util.List;

import butterknife.BindView;

public class MarketSearchListActivity extends MVPActivity<MarketSearchListPresenter> {
    @BindView(R.id.recyclerView)
    RecyclerView recyclerView;
    @BindView(R.id.smartRefreshLayout)
    SmartRefreshLayout smartRefreshLayout;
    @BindView(R.id.cancel)
    TextView cancel;
    BaseAdapter<MarketData> adapter;
    int page = 1;
    int pageSize = 10;
    @BindView(R.id.market_search)
    ImageView marketSearch;
    @BindView(R.id.edit_coin_name)
    EditText editCoinName;
    private boolean isMarket;  // 行情页面 true
    private int position;  // 记录交易融资还是融券
    private String code;  // 查询的币种信息

    @Override
    public int getLayoutId() {
        return R.layout.activity_market_search_list;
    }

    @Override
    public MarketSearchListPresenter getPresenter() {
        return new MarketSearchListPresenter();
    }

    @Override
    public void initView() {
        isMarket = getIntent().getBooleanExtra("isMarket", true);
        position = getIntent().getIntExtra("position", 0);
        code = getIntent().getStringExtra("code");
        editCoinName.setText(code);
        initRefreshLayout(smartRefreshLayout);
        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        recyclerView.addItemDecoration(new DividerLineItemDecoration(this)
                .setPaintWidth(2)
                .setFirstDraw(false)
                .setDividerColor(color(R.color.lineColor))
        );
        adapter = new BaseAdapter<MarketData>(R.layout.search_item, null, recyclerView, true) {
            @Override
            public void bind(ViewHolder holder, MarketData item) {
                holder.setVisible(R.id.notice_more, isMarket ? true : false);
                holder.setText(R.id.coin_name, item.getPname())
                        .setText(R.id.coin_english, item.getCode());
                holder.getView(R.id.notice_more).setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        mPresenter.addOptional(item.getCode(), true);
                    }
                });
                if (!isMarket) {
                    holder.getView(R.id.ll_item).setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            MainActivity.startToIndex(MarketSearchListActivity.this, 2, position, item);
                        }
                    });
                }
            }
        };
        smartRefreshLayout.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh(RefreshLayout refreshLayout) {
                page = 1;
                mPresenter.getNoticeList(code, false);
            }
        });
        ClickUtil.click(cancel, view -> {
            finish();
        });
        ClickUtil.click(marketSearch, view -> {
            if (TextUtils.isEmpty(getText(editCoinName))) {
                ToastUtils.show("请输入股票名称或代码");
                return;
            }
            page = 1;
            mPresenter.getNoticeList(getText(editCoinName), false);
        });

        editCoinName.setOnEditorActionListener((v, actionId, event) -> {
            if (TextUtils.isEmpty(getText(editCoinName))) {
                ToastUtils.show("请输入股票名称或代码");
                return true;
            }
            page = 1;
            mPresenter.getNoticeList(getText(editCoinName), false);
            return false;
        });
    }

    @Override
    public void initData() {
        mPresenter.getNoticeList(code, false);
    }


    public void setData(List<MarketData> data) {
        if (data != null) {
            if (page == 1) {
                adapter.setNewData(data);
            } else {
                adapter.addData(data);
            }
        } else {
            adapter.setNewData(null);
        }
    }

    public void finishRefresh() {
        smartRefreshLayout.finishLoadMore();
        smartRefreshLayout.finishRefresh();
    }


    public static void start(Context context, boolean isMarket, int position, String code) {
        Intent intent = new Intent(context, MarketSearchListActivity.class);
        intent.putExtra("isMarket", isMarket);
        intent.putExtra("position", position);
        intent.putExtra("code", code);
        context.startActivity(intent);
    }

}
